package com.kmproject.haifu.mvp.ui.adapter;

import android.content.Context;
import android.support.annotation.Nullable;
import android.support.v4.content.ContextCompat;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.jess.arms.utils.ArmsUtils;
import com.kmproject.haifu.R;
import com.kmproject.haifu.app.utils.CustomGridItemDecoration;
import com.kmproject.haifu.app.utils.ExtensionUtilKt;
import com.kmproject.haifu.mvp.model.entity.beans.HomeNewsBean;

import java.util.List;
import java.util.Objects;

import static com.luliang.shapeutils.DevShapeUtils.getContext;

public class HomeNewsAdapter extends BaseQuickAdapter<HomeNewsBean, BaseViewHolder> {
    private Context mContext;

    public HomeNewsAdapter(@Nullable List<HomeNewsBean> data, Context mContext) {
        super(R.layout.item_home_news, data);
        this.mContext = mContext;
    }

    @Override
    protected void convert(BaseViewHolder helper, HomeNewsBean item) {
        helper.setText(R.id.tv_news_summary, item.getHeadlineTitle());
        if (item.getIsAdvertisement() == 0) {//是否是广告:0-否，1-是
            helper.getView(R.id.ll_news_layout_1).setVisibility(View.VISIBLE);
            helper.getView(R.id.ll_news_layout_2).setVisibility(View.GONE);
            helper.setText(R.id.tv_news_view_count, String.format(mContext.getString(R.string.home_news_view_count_fm), item.getViewCount()));
            helper.setText(R.id.tv_news_comment_count, String.format(mContext.getString(R.string.home_news_comment_fm), item.getCommentCount()));
            helper.setText(R.id.tv_news_release_time, item.getPublishTime());
        } else {
            helper.getView(R.id.ll_news_layout_1).setVisibility(View.GONE);
            helper.getView(R.id.ll_news_layout_2).setVisibility(View.VISIBLE);
            TextView tv_news_view_detail = helper.getView(R.id.tv_news_view_detail);
            tv_news_view_detail.setOnClickListener(v -> {
                // todo 跳转WebView
                item.getLink();
            });
        }

        switch (item.getHeadlineType()) {//:0-无图,1-一张图片，3-三张图片
            case 0:
                helper.getView(R.id.rv_news_img).setVisibility(View.GONE);
                helper.getView(R.id.imv_news_img).setVisibility(View.GONE);
                break;
            case 1:
                helper.getView(R.id.rv_news_img).setVisibility(View.GONE);
                ImageView imv_news_img = helper.getView(R.id.imv_news_img);
                imv_news_img.setVisibility(View.VISIBLE);
                if (item.getImages() != null && item.getImages().size() > 0) {
                    ExtensionUtilKt.loadImage(imv_news_img, item.getImages().get(0).getImageUrl());
                } else {
                    imv_news_img.setVisibility(View.GONE);
                }
                break;
            case 2:
            case 3:
                helper.getView(R.id.imv_news_img).setVisibility(View.GONE);
                RecyclerView rv_news_img = helper.getView(R.id.rv_news_img);
                rv_news_img.setVisibility(View.VISIBLE);
                if (item.getImages() != null && item.getImages().size() > 0) {
                    rv_news_img.addItemDecoration(new CustomGridItemDecoration.Builder(Objects.requireNonNull(getContext())).setVerticalSpan(10f).
                            setHorizontalSpan(10f).setColor(ContextCompat.getColor(getContext(), R.color.white)).setShowLastLine(false).build());
                    ArmsUtils.configRecyclerView(rv_news_img, new GridLayoutManager(mContext, 3));
                    rv_news_img.setItemAnimator(new DefaultItemAnimator());

                    rv_news_img.setAdapter(new BaseQuickAdapter<HomeNewsBean.ImagesBean, BaseViewHolder>(R.layout.item_view_img, item.getImages()) {
                        @Override
                        protected void convert(BaseViewHolder holder, HomeNewsBean.ImagesBean model) {
                            ImageView imv_img = holder.getView(R.id.imv_img);
                            ExtensionUtilKt.loadImage(imv_img, model.getImageUrl());
                        }
                    });
                } else {
                    rv_news_img.setVisibility(View.GONE);
                }
                break;
            default:
                helper.getView(R.id.rv_news_img).setVisibility(View.GONE);
                helper.getView(R.id.imv_news_img).setVisibility(View.GONE);
        }
    }
}
